<template>
  <div class="panel">
    <div class="header">
      <p>
        <span>数据看板</span>
      </p>
      <div class="top_right">
        <div class="picker_select">
          <el-select
            v-model="yyname"
            clearable
            filterable
            placeholder="请选择"
            size="small"
          >
            <el-option
              v-for="items in yylist.list"
              :key="items.id"
              :label="items.applicationName"
              :value="items.id"
            ></el-option>
          </el-select>
        </div>

        <el-date-picker
          v-model="topTime"
          type="daterange"
          :picker-options="pickerOptions2"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right"
          format="yyyy- MM- dd"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
        <el-button type="primary" size="mini" @click="dataModel()"
          >刷新</el-button
        >
      </div>
    </div>

    <el-row :gutter="40" class="panel-group">
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" @click="handleSetLineChartData('newVisitis')">
          <div class="card-panel-icon-wrapper icon-people">
            <!-- <svg-icon icon-class="peoples" class-name="card-panel-icon" /> -->
            <img src="../../../../assets/dash1.png" alt="" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">新增应用数</div>
            <count-to
              :start-val="0"
              :end-val="datalist.applicationAddCount"
              :duration="2600"
              class="card-panel-num"
            />
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" @click="handleSetLineChartData('messages')">
          <div class="card-panel-icon-wrapper icon-message">
            <!-- <svg-icon icon-class="message" class-name="card-panel-icon" /> -->
            <img src="../../../../assets/dash2.png" alt="" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">新增客户数</div>
            <count-to
              :start-val="0"
              :end-val="datalist.memberAddCount"
              :duration="3000"
              class="card-panel-num"
            />
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" @click="handleSetLineChartData('purchases')">
          <div class="card-panel-icon-wrapper icon-money">
            <!-- <svg-icon icon-class="money" class-name="card-panel-icon" /> -->
            <img src="../../../../assets/dash3.png" alt="" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">总应用数</div>
            <count-to
              :start-val="0"
              :end-val="datalist.applicationCount"
              :duration="3200"
              class="card-panel-num"
            />
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" @click="handleSetLineChartData('shoppings')">
          <div class="card-panel-icon-wrapper icon-shopping">
            <!-- <svg-icon icon-class="shopping" class-name="card-panel-icon" /> -->
            <img src="../../../../assets/dash4.png" alt="" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">总客户数</div>
            <count-to
              :start-val="0"
              :end-val="datalist.memberCount"
              :duration="3600"
              class="card-panel-num"
            />
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import api from "../../api/common.js";
import CountTo from "vue-count-to";
export default {
  components: {
    CountTo,
  },
  data() {
    return {
      topTime: [this.getTimeAlready()[1], this.getTimeAlready()[1]],
      yyname: "",
      value4: "",
      pickerOptions2: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      yylist: [],
      datalist: "", //数据看板
    };
  },
  created() {
    // 应用列表
    api.selectType().then((res) => {
      if (res.code == 0) {
        this.yylist = res.data;
      } else {
        this.$message.error(res.message);
      }
    });
    this.dataModel();
  },
  methods: {
     // 获取将近前一个月的时间
    getTimeAlready() {
        const end = new Date();
        const start = new Date();
        const time = start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
        const endTime = /\d{4}-\d{1,2}-\d{1,2}/g.exec(this.$moment(end))[0];
        const startTime = /\d{4}-\d{1,2}-\d{1,2}/g.exec(this.$moment(time))[0];
        return [startTime, endTime];
    },
    // 数据看板
    dataModel() {
      let beginTime = "";
      let endTime = "";
      if (this.topTime) {
        beginTime = this.topTime[0] + " 00:00:00";
        endTime = this.topTime[1] + " 23:59:59";
      }

      this.axios
        .post("/member/v1/member/application/count", {
          applicationId: this.yyname,
          applicationName: "",
          endTime: endTime,
          startTime: beginTime,
        })
        .then((res) => {
          // console.log(res);
          if (res.code == 0) {
            this.datalist = res.data;
          } else {
            this.$message.error(res.message);
          }
        });
    },
    handleSetLineChartData(type) {
      this.$emit("handleSetLineChartData", type);
    },
  },
};
</script>

<style lang="scss" scoped>
.panel-group {
  margin-bottom: 32px;
  margin-left: 0 !important;
  margin-right: 0 !important;
  background: #fff;
  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    // box-shadow: 4px 4px 40px rgba(0, 0, 0, 0.05);
    border-color: rgba(0, 0, 0, 0.05);
    display: flex;
    justify-content: space-around;
    .icon-people {
      color: #40c9c6;
    }

    .icon-message {
      color: #36a3f7;
    }

    .icon-money {
      color: #f4516c;
    }

    .icon-shopping {
      color: #34bfa3;
    }

    .card-panel-icon-wrapper {
      transition: all 0.38s ease-out;
      border-radius: 6px;
      margin-left: 0;
      img {
        width: 75px;
        height: 75px;
        margin: 18px 0;
      }
    }

    .card-panel-icon {
      // float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}

@media (max-width: 550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}
.header {
  height: 30px;
  // line-height: 30px;
  padding: 10px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
  // min-width: 1000px;
  background-color: #fff;

  .el-button {
    margin-left: 10px;
  }

  > p:first-child {
    font-size: 16px;
    font-weight: bold;
    border-left: 3px solid #2d7ddf;
    padding-left: 10px;
    height: 14px;
    line-height: 14px;
  }

  .timer {
    font-weight: normal;
    display: inline-block;
    margin-left: 15px;
    color: #999999;
    font-size: 14px;
  }

  .el-input__inner {
    width: 260px;
    height: 30px;
    line-height: 30px !important;
  }

  .picker {
    display: inline-block;
    vertical-align: middle;
    margin-right: 10px;
  }
  .top_right {
    display: flex;
    justify-content: flex-end;
    align-items: center;
  }
  /deep/.el-input__icon {
    margin-top: -10px;
  }
  // /deep/.el-range-separator{
  //   height: 25px;
  // }
  /deep/ .el-date-editor .el-range-separator {
    line-height: 23px !important;
  }
}
.picker_select {
  margin-right: 10px;
  /deep/.el-input--small .el-input__inner {
    height: 30px;
    height: 30px;
  }
  /deep/.el-input__icon {
    margin: 0 !important;
  }
}
</style>
